//package com.ruoyi.cluster;
//
//import javax.annotation.PreDestroy;
//
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.state.ConnectionState;
//import org.apache.zookeeper.CreateMode;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.ApplicationArguments;
//import org.springframework.boot.ApplicationRunner;
//import org.springframework.stereotype.Component;
//
//import cn.hutool.core.net.NetUtil;
//
///**
//* @ClassName: ServerWorkWatcher 
//* @Description: TODO(抢占工作节点) 
//* @author 晏大兵 
//* @date 2025年3月28日 上午11:00:05 
// */
//@Component
//public class ServerWorkWatcher implements ApplicationRunner {
//
//	@Autowired
//	ZookeeperConfig zookeeperConfig;
//	
//	@Autowired
//    private CuratorFramework client;
//	
//	private String NODE_PATH = "/podCluster/";
//	
//	
//	@Override
//	public void run(ApplicationArguments args) throws Exception {
//		if(!zookeeperConfig.isEnable()) {
//			return ;
//		}
//		NODE_PATH = NODE_PATH + zookeeperConfig.getPodKey();
//		createEphemeralNode();
//        addConnectionListener();
//	}
//
//    
//
//
//    private void createEphemeralNode() throws Exception {
//    	String ipv4 = NetUtil.getLocalhostStr();
//    	System.out.println("ipv4 | " + ipv4);
//        if (client.checkExists().forPath(NODE_PATH) == null) {
//            client.create().creatingParentsIfNeeded()
//                    .withMode(CreateMode.EPHEMERAL)
//                    .forPath(NODE_PATH, "GuardData".getBytes());
//        }
//    }
//
//    private void addConnectionListener() {
//        client.getConnectionStateListenable().addListener((client, newState) -> {
//            if (newState == ConnectionState.RECONNECTED) {
//                try {
//                    createEphemeralNode();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }
//
//    @PreDestroy
//    public void cleanUp() throws Exception {
//        if (client.checkExists().forPath(NODE_PATH) != null) {
//            client.delete().guaranteed().forPath(NODE_PATH);
//        }
//    }
//    
//}
